import 'package:cbec_app/main.dart';
import 'package:cbec_app/pages/main/view/main_page.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../pages/main/controller/main_ctr.dart';
import '../../utils/EventMgr.dart';
import 'coupon_shape_border.dart';

/**
 * 单个红包
 */
class RedpackItem extends StatefulWidget {
  // final String price; //金额
  // final String needMoney; //满多少可使用
  // final String start_time; //开始时间
  // final String end_time; //结束时间
  // final String title; //优惠券标题
  // final String sub_title; //副标题
  // final String desc; //使用说明

  // final int status;
  // final String status_text;
  final dynamic item;

  final VoidCallback? refund;

  const RedpackItem({super.key, required this.item, required this.refund

      // required this.price,
      // required this.needMoney,
      // required this.start_time,
      // required this.end_time,
      // required this.title,
      // required this.sub_title,
      // required this.desc,
      // required this.status,
      // required this.status_text
      });

  @override
  State<RedpackItem> createState() => _CouponItemState();
}

class _CouponItemState extends State<RedpackItem> {
  bool _is_open = false;
  late Icon _icons;
  late TextStyle amountColor;
  late TextStyle commentColor;
  late TextStyle titleColor;
  late TextStyle subTitleColor;

  late TextStyle descdColor;
  late TextStyle buttonTextColor;

  late ButtonStyle buttonStyle;
  late Color iconColor;
  late BoxDecoration boxdecoration;

  @override
  Widget build(BuildContext context) {
    //状态渲染
    if (widget.item['status'] == 0) {
      //待使用
      boxdecoration = const BoxDecoration(
        color: Colors.transparent,
        image: DecorationImage(
          image: AssetImage("assets/images/redpack_bg.png"),
          fit: BoxFit.cover,
        ),
      );

      amountColor = const TextStyle(fontSize: 22, fontWeight: FontWeight.w700, color: Color.fromRGBO(210, 152, 64, 1));
      commentColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Color.fromRGBO(210, 152, 64, 1));
      titleColor = const TextStyle(fontSize: 14, fontWeight: FontWeight.w700, color: Color.fromRGBO(251, 241, 216, 1));
      subTitleColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Color.fromRGBO(251, 241, 216, 1));
      descdColor = const TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Color.fromRGBO(251, 241, 216, 1));
      buttonTextColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Color.fromRGBO(251, 241, 216, 1));
      buttonStyle = ButtonStyle(
        backgroundColor: WidgetStateProperty.all(Tools.fromHex("#F83F2D")),
        side: WidgetStateProperty.all(const BorderSide(color: Color.fromRGBO(251, 241, 216, 1), width: 1.0)),
      );

      iconColor = const Color.fromRGBO(251, 241, 216, 1);
    } else {
      //不可使用
      boxdecoration = const BoxDecoration(
        color: Colors.white,
      );

      amountColor = const TextStyle(fontSize: 22, fontWeight: FontWeight.w700, color: Colors.black);
      commentColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Colors.black);
      titleColor = const TextStyle(fontSize: 14, fontWeight: FontWeight.w700, color: Color.fromRGBO(51, 51, 51, 1));
      subTitleColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Color.fromRGBO(102, 102, 102, 1));
      descdColor = const TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Color.fromRGBO(102, 102, 102, 1));
      buttonTextColor = const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Colors.white);

      buttonStyle = ButtonStyle(
          //
          backgroundColor: WidgetStateProperty.all(const Color.fromRGBO(204, 204, 204, 1)) //const Color.fromRGBO(254, 85, 104, 1)
          );
      iconColor = const Color.fromRGBO(102, 102, 102, 1);
    }

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
            // height: 90,
            decoration: boxdecoration,
            // constraints: BoxConstraints(maxHeight: 200.w),
            margin: EdgeInsets.only(top: 10.w),
            padding: EdgeInsets.only(top: 5.w, bottom: 5.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
                Row(
                  children: [
                    //左
                    Container(
                      width: 80.w,
                      height: 80.h,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("￥${Tools.formatNumber(widget.item['money'])}", style: amountColor),
                          //  Text("满${widget.needMoney}可用",style:const TextStyle(fontSize: 12,color: Colors.white))
                          Text(widget.item['content'], style: commentColor)
                        ],
                      ),
                    ),
                    //中
                    Container(
                      height: 80.h,
                      width: 170.w,
                      margin: EdgeInsets.fromLTRB(10.w, 0.w, 20.w, 0.w),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(widget.item['redPacketName'], style: titleColor),
                          Text(widget.item['goodsType'] ?? '未知', style: subTitleColor),
                          Text(widget.item['useDate'] ?? '', style: subTitleColor),
                        ],
                      ),
                    ),
                  ],
                ),

                //右【按钮】

                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(right: 8.w),
                    child: Column(
                      children: [
                        Container(
                            // padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 5),
                            height: 30,
                            child: TextButton(
                              style: buttonStyle,
                              onPressed: () {
                                // widget.item['status']==0?Get.offAllNamed(Routes.main):();
                                if (widget.item['status'] == 0) {
                                  Get.until((route) => Get.currentRoute == Routes.main);
                                  EventMgr.send(EventType.exchangeNav, MainController.mainBottomNavRouteNameHome);
                                }
                              },
                              child: Text(
                                widget.item['status'] == 0
                                    ? '去使用'
                                    : widget.item['status'] == 1
                                        ? '已使用'
                                        : widget.item['status'] == 2
                                            ? '已过期'
                                            : '',
                                style: buttonTextColor,
                              ),
                            )),
                        // ((widget.item['status']==0) && (widget.item['payStatus']==1) && (widget.item['refundStatus']==0))?
                        // Container(
                        //     margin: EdgeInsets.only(top: 5.w),
                        //     height: 24,
                        //     child: ElevatedButton(
                        //       style: buttonStyle,
                        //       onPressed: widget.refund,
                        //       child: Text("去退款", style: buttonTextColor),
                        // ))
                        //     :const SizedBox(),
                      ],
                    ),
                  ),
                )
              ],
            )),
        Visibility(
            visible: _is_open, //couponCtr.isCloses.value,
            child: Container(
              padding: EdgeInsets.only(left: 110.w),
              alignment: Alignment.topLeft,
              width: 375.w,
              margin: EdgeInsets.fromLTRB(10.w, 10.w, 5.w, 5.w),
              // color: Colors.grey,
              child: Text(widget.item['goodsType'] ?? '', style: const TextStyle(fontSize: 11, fontWeight: FontWeight.w300, color: Color.fromRGBO(102, 102, 102, 1))),
            ))
      ],
    );
  }
}
